package com.cloudwise.controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/9/9 11:56
 * @description：第一个控制器
 * @modified By：
 * @version: 1.0
 */
public class MyServlet extends HttpServlet {

    public MyServlet() {
        System.out.println("servlet的构造方法被调用了");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("servlet初始化了");
    }

    @Override
    public void destroy() {
        System.out.println("servlet销毁了");
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //获取上下文内容，servlet容器
        ServletContext servletContext = super.getServletContext();
        //获取全局变量
        String encoding = servletContext.getInitParameter("encoding");
        System.out.println("全局参数encoding："+encoding);
        //获取局部参数
        String hobby = getInitParameter("hobby");
        System.out.println("MyServlet的爱好："+hobby);
        //获取servlet的名称
        String servletName = super.getServletName();
        System.out.println("servlet的名称："+servletName);
        //获取初始化参数名称
        Enumeration<String> initParameterNames = getInitParameterNames();
        System.out.println("遍历初始化参数名称");
        while (initParameterNames.hasMoreElements()){
            System.out.println(initParameterNames.nextElement());
        }
        //获取所有的参数名称
        Enumeration<String> parameterNames = req.getParameterNames();
        System.out.println("遍历请求参数名称");
        while (parameterNames.hasMoreElements()){
            System.out.println(parameterNames.nextElement());
        }
        System.out.println("我接收到了get请求");
        System.out.println("参数：name:"+req.getParameter("name"));
        //响应回浏览器
        resp.getWriter().write("<h1>"+req.getParameter("name")+"</h1>");
        //响应所有的userinfo数据到浏览器,table格式
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }


}
